package com.lex.crossgate.admin.modules.sys.service.imp;

import com.lex.crossgate.common.constants.R;;
import com.lex.crossgate.admin.modules.oauth2.TokenGenerator;
import com.lex.crossgate.admin.modules.sys.dao.*;
import com.lex.crossgate.admin.modules.sys.entity.SysUser;
import com.lex.crossgate.admin.modules.sys.entity.SysUserToken;
import com.lex.crossgate.admin.modules.sys.service.SysUserService;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * java类简单作用描述
 *
 * @ProjectName: crossgate
 * @Package: com.lex.crossgate.admin.modules.sys.service.imp
 * @ClassName: ${TYPE_NAME}
 * @Description: java类作用描述
 * @Author: 李贝留斯
 * @CreateDate: 2018/5/2 15:55
 * @Version: 1.0
 **/
@Service("sysUserService")
public class SysUserServiceImpl extends ServiceImpl<SysUserDao, SysUser> implements SysUserService{

    @Autowired
    private SysUserDao sysUserDao;

    @Autowired
    private SysRoleDao sysRoleDao;

    @Autowired
    private SysMenuDao sysMenuDao;

    @Autowired
    private SysUserTokenDao sysUserTokenDao;

    /**
     * token过期时间，12小时
     */
    private final static int TOKEN_EXPIRE = 1000 * 60 * 60 * 12;

    /**
     * @description: 获取用户列表
     * @author 李贝留斯
     * @date 2018/5/8 21:30
     */
    public List<SysUser> getSysUser(String userName,String mobile) {
        EntityWrapper entityWrapper = new EntityWrapper();
        if (StringUtils.isNotEmpty(userName))
            entityWrapper.like("user_name",userName);
        if (StringUtils.isNotEmpty(mobile))
            entityWrapper.like("mobile",mobile);
        List<SysUser> userList = sysUserDao.selectList(entityWrapper);
        return userList;
    }

    /**
     * TODO: 前端验证用户名是否重复方法
     * @author 李贝留斯
     * @date 2018/5/18 16:08
     */
    public int getUserByUserName(String userName) {
        EntityWrapper entityWrapper = new EntityWrapper();
        if (StringUtils.isNotEmpty(userName))
            entityWrapper.like("user_name",userName);
        return sysUserDao.selectCount(entityWrapper);

    }

    public SysUser getByUserName(String userName) {
        return sysUserDao.selectByUserName(userName);
    }

    public Set<String> listUserRoles(String userId) {
        List<String> roles = sysRoleDao.listUserRoles(userId);
        Set<String> rolesSet = new HashSet<>();
        for(String role : roles) {
            if(StringUtils.isNotBlank(role)) {
                rolesSet.addAll(Arrays.asList(role.trim().split(",")));
            }
        }
        return rolesSet;
    }

    @Override
    public Set<String> listUserPerms(String userId) {
        List<String> perms = sysMenuDao.listUserPerms(userId);
        Set<String> permsSet = new HashSet<>();
        for(String perm : perms) {
            if(StringUtils.isNotBlank(perm)) {
                permsSet.addAll(Arrays.asList(perm.trim().split(",")));
            }
        }
        return permsSet;
    }

    @Override
    public R saveUserToken(String userId) {
        //生成token
        String token = TokenGenerator.generateValue();
        //当前时间
        Date now = new Date();
        Date gmtExpire = new Date(now.getTime() + TOKEN_EXPIRE);
        //根据ID查询token
        SysUserToken userToken = sysUserTokenDao.selectById(userId);
        if(userToken == null) {
            userToken = new SysUserToken();
            userToken.setToken(token);
            userToken.setGmtExpire(gmtExpire);
            userToken.setGmtModified(now);
            sysUserTokenDao.insert(userToken);
        } else {
            userToken.setToken(token);
            userToken.setGmtExpire(gmtExpire);
            userToken.setGmtModified(now);
            sysUserTokenDao.updateById(userToken);
        }
        R r = R.ok().put("token", userToken.getToken()).put("expire", userToken.getGmtExpire());
        return r;
    }
}
